package com.springboot.framework.controller;

import com.springboot.framework.annotation.ACS;
import com.springboot.framework.controller.request.ShopInsertBean;
import com.springboot.framework.vo.ResponseVO;
import com.springboot.framework.exception.BusinessException;
import com.springboot.framework.model.ShopModel;
import com.springboot.framework.service.ShopService;
import com.springboot.framework.utils.ResponseVoUtil;
import com.springboot.framework.vo.ShopVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author SWF
 * @Date 2019/6/6 14:54
 **/
@Api(description = "店铺")
@RestController
@RequestMapping("/shop")
public class ShopController {
    @Resource
    private ShopService shopService;

    @ApiOperation(value = "获取所有商铺", notes = "获取所有商铺")
    @GetMapping("/list")
    public ResponseVO list() {
        List<ShopVO> shopVOS = convertShopVOFromShopModel(shopService.listNormal());
        return ResponseVoUtil.success(shopVOS);
    }

    @ApiOperation(value = "获取附近商铺", notes = "获取附近商铺")
    @GetMapping("/listByAddress")
    public ResponseVO listByAddress(@RequestParam BigDecimal longitude ,@RequestParam BigDecimal latitude ,@RequestParam Integer distance) {
        List<ShopVO> shopVOS = convertShopVOFromShopModel(shopService.listByDistance(longitude,latitude,distance));
        return ResponseVoUtil.success(shopVOS);
    }

    @ACS(allowAnonymous = true)
    @ApiOperation(value = "新增", notes = "新增")
    @PostMapping("/seller/insert")
    public ResponseVO insert(@RequestBody @Valid ShopInsertBean bean, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new BusinessException(bindingResult.getFieldError().getDefaultMessage());
        }
        ShopModel shopModel = convertShopModel(bean);
        //productModel.setCreateBy();
        return ResponseVoUtil.success(shopService.insert(shopModel));
    }



    private ShopVO convertShopVOFromShopModel(ShopModel shopModel) {
        if (shopModel == null) {
            return null;
        }
        ShopVO shopVO = new ShopVO();
        BeanUtils.copyProperties(shopModel, shopVO);
        return shopVO;
    }

    private List<ShopVO> convertShopVOFromShopModel(List<ShopModel> shopModels) {
        if (shopModels == null) {
            return null;
        }
        return shopModels.stream()
                .map(e -> convertShopVOFromShopModel(e))
                .collect(Collectors.toList());
    }

    private ShopModel convertShopModel(ShopInsertBean bean) {
        ShopModel shopModel = new ShopModel();
        BeanUtils.copyProperties(bean, shopModel);
        return shopModel;
    }
}
